/* 这里面是整个系统业务相关的js代码 */
//提取URL中参数为对象
function parseQueryString(url) {
    var str = url.split('?')[1];
    var result = {};
    var temp = (str || '').split('&');
    for (var i = 0; i < temp.length; i++) {
        var temp2 = (temp[i] || '').split('=');
        result[temp2[0]] = temp2[1];
    }
    return result;
}

//校验手机号码
function checkPhone(telphone) {
    var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (!myreg.test(telphone)) {
        return false;
    } else {
        return true;
    }
};

/**
 * 表格中显示图片，鼠标移动可放大
 * */
function hoverOpenImg() {
    var img_show = null;

    $("td img").hover(function () {
        var img = "<img class='img_msg' src='" + $(this).attr('src') + "' style='width:100%;' />";
        img_show = layer.tips(img, this, {
            tips: [2, 'rgba(255,255,255,.8)'],
            area: ['15%']
        });
    }, function () {
        layer.close(img_show);
    });
}

/**
 * 关闭窗口
 * */
function layer_close() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

/**
 * 获取URL参数
 * @param {any} variable 参数名
 * @returns {any} 返回参数值
 */
function getUrlParam(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] === variable) { return pair[1]; }
    }
    return "";
}

/**
 * 计算时间差
 * @param {any} dateTime 到期时间
 * @param {any} value    时间差
 * @returns {boolean} 布尔值
 */
function clacDateTime(dateTime, value) {
    var curTime = new Date();
    var endTime = new Date(Date.parse(dateTime) - (1000 * 86400 * value));
    return curTime > endTime;
}

/**
 * 删除数据
 * @param {any} url 请求地址
 * @param {any} id  数据Id
 * @param {any} index 当前的弹出层
 * @param {any} obj   表格数据
 */
function deleteData(url, id, index, obj) {
    let indexLoad = "";
    $.ajax({
        url,
        type: 'POST',
        dataType: 'json',
        data: { id },
        beforeSend: function () {
            indexLoad = layer.load(1, { shade: [0.3, '#000'] });
        },
        success: function (res) {
            if (res.code === 200) {
                layer.msg(res.data, { icon: 1, time: 1000 }, function () {
                    obj.del();
                });
            } else {
                layer.msg(res.errorMsg, { icon: 2, time: 2000 });
            }
        },
        error: function () {
            layer.msg('网络错误', { icon: 2, time: 2000 });
        },
        complete: function () {
            layer.close(index);
            layer.close(indexLoad);
        }
    });
}

/**
 * 批量删除
 * @param {any} url 批量删除的请求地址
 * @param {any} checkStatus 选择的数据
 * @param {ayn} table 表格
 */
function batchDelete(url, checkStatus, table) {
    //批量删除
    if (checkStatus.data.length > 0) {
        layer.confirm('确认要批量删除这些数据吗？', function () {
            let index = "";
            let idArr = [];
            checkStatus.data.forEach(item => {
                idArr.push(item.id)
            })
            $.ajax({
                url,
                type: 'POST',
                data: { selectedIds: idArr },
                dataType: 'json',
                beforeSend: function () {
                    index = layer.load(1, { shade: [0.3, '#000'] })
                },
                success: function (res) {
                    if (res.code === 200) {
                        layer.msg(res.data, { icon: 1, time: 1000 }, function () {
                            table.reload();
                        });
                    } else {
                        layer.msg(res.errorMsg, { icon: 2, time: 2000 });
                    }
                },
                error: function () {
                    layer.msg('网络错误', { icon: 2, time: 2000 });
                },
                complete: function () {
                    layer.close(index);
                }
            });
        });
    } else {
        layer.msg('请选择要删除的数据！', { icon: 0, time: 1000 });
        return;
    }
}

/**
 * 批量确认
 * @param {any} url   请求地址
 * @param {any} checkStatus  选中的数据
 * @param {any} table   表格
 */
function batchConfirm(url, checkStatus, table) {
    if (checkStatus.data.length > 0) {
        layer.confirm('确认要批量确认完成这些订单吗？', function () {
            let index = "";
            let idArr = [];
            checkStatus.data.forEach(item => {
                idArr.push(item.id)
            })
            $.ajax({
                url,
                type: 'POST',
                data: { selectedIds: idArr },
                dataType: 'json',
                beforeSend: function () {
                    index = layer.load(1, { shade: [0.3, '#000'] })
                },
                success: function (res) {
                    if (res.code === 200) {
                        layer.msg(res.data, { icon: 1, time: 1000 }, function () {
                            table.reload();
                        });
                    } else {
                        layer.msg(res.errorMsg, { icon: 2, time: 2000 });
                    }
                },
                error: function () {
                    layer.msg('网络错误', { icon: 2, time: 2000 });
                },
                complete: function () {
                    layer.close(index);
                }
            });
        });
    } else {
        layer.msg('请选择要确认完成的订单数据！', { icon: 0, time: 1000 });
        return;
    }
}

function member_show(title, url, id) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url + "?id=" + id,
        area: ['400px', '500px']
    });
}

/**
 * 保存数据
 * @param {any} url  请求地址
 * @param {any} data 表单数据
 */
function savaData(url, data) {
    let index = "";
    $.ajax({
        url,
        type: 'POST',
        dataType: 'json',
        data,
        beforeSend: function () {
            index = layer.load(1, { shade: [0.3, '#000'] });
        },
        success: function (res) {
            if (res.code === 200) {
                layer.msg(res.data, { icon: 1, time: 1000 }, function () {
                    parent.location.reload(); //刷新父窗口
                });
            } else {
                layer.msg(res.errorMsg, { icon: 2, time: 2000 });
            }
        },
        error: function () {
            layer.msg('网络错误', { icon: 2, time: 2000 });
        },
        complete: function () {
            layer.close(index);
        }
    });
}

/**
 * 添加数据
 * @param {string} title 窗口标题
 * @param {string} url   请求的url
 * @param {boolean} isFull 是否全屏，全屏可不填写窗口宽度和高度
 * @param {number} wndWh  窗口宽度
 * @param {number} wndHg  窗口高度
 */
function add_data(title, url, isFull, wndWh = 0, wndHg = 0) {
    //全屏
    if (isFull) {
        let index = layer.open({
            type: 2,
            title,
            content: url,
        })
        layer.full(index);
    } else {
        layer.open({
            type: 2,
            title,
            content: url,
            area: [wndWh + 'px', wndHg + 'px']
        })
    }
}

/**
 * 修改数据
 * @param {string} title 窗口标题
 * @param {string} url   请求的url
 * @param {number} id    请求的参数Id
 * @param {boolean} isFull  是否全屏，全屏可不填写窗口宽度和高度
 * @param {number} wndWh  窗口宽度
 * @param {number} wndHg  窗口高度
 */
function edit_data(title, url, id, isFull, wndWh = 0, wndHg = 0) {
    let strUrl = url + (url.indexOf('?') < 0 ? '?' : '&') + 'id=' + id;

    if (isFull) {
        let index = layer.open({
            type: 2,
            title,
            content: strUrl
        });
        layer.full(index)
    } else {
        layer.open({
            type: 2,
            title,
            content: strUrl,
            area: [wndWh + 'px', wndHg + 'px']
        })
    }
}

/*密码-修改*/
function change_password(title, url, id) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url + "?id=" + id,
        area: ['800px', '350px']
    });
}